package vip.ola.service.impl;

import org.apache.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import vip.ola.core.common.util.RedisUtil;
import vip.ola.core.entity.SysConfig;
import vip.ola.core.entity.SysConfigExample;
import vip.ola.core.service.ISysConfigService;
import vip.ola.service.dao.mapper.SysConfigMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static vip.ola.core.common.constant.CacheKey.HALF_HOURS;
import static vip.ola.core.common.constant.CacheKey.SYS_CONFIG;

/**
 * @author: dingzhiwei
 * @date: 2018/4/27
 * @description:
 */
@Service(interfaceName = "vip.ola.core.service.ISysConfigService", version = "1.0.0", retries = -1)
public class SysConfigServiceImpl implements ISysConfigService {

    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<SysConfig> select(String type) {
        SysConfigExample example = new SysConfigExample();
        example.setOrderByClause("orderNum ASC");
        SysConfigExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(type);
        return sysConfigMapper.selectByExample(example);
    }

    @Override
    public SysConfig select(String type, String code) {
        SysConfigExample example = new SysConfigExample();
        example.setOrderByClause("orderNum ASC");
        SysConfigExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(type);
        criteria.andCodeEqualTo(code);
        List<SysConfig> sysConfigs = sysConfigMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(sysConfigs)) {
            return sysConfigs.get(0);
        }
        return null;
    }

    @Override
    public JSONObject getSysConfigObj(String type) {
        return new JSONObject(redisUtil.hmget(SYS_CONFIG + type, () -> {
            Map<String, Object> map = new HashMap<>();
            List<SysConfig> sysConfigList = select(type);
            if (CollectionUtils.isEmpty(sysConfigList)) {
                return map;
            }
            for (SysConfig sysConfig : sysConfigList) {
                map.put(sysConfig.getCode(), sysConfig.getValue());
            }
            return map;
        }, HALF_HOURS));
    }

    @Override
    public String getSysValue(String type, String code) {
        JSONObject object = getSysConfigObj(type);
        return object.getString(code);
    }

    @Override
    public int updateAll(String type, List<SysConfig> sysConfigList) {
        if (CollectionUtils.isEmpty(sysConfigList)) return 0;
        int count = 0;
        for (SysConfig sysConfig : sysConfigList) {
            int result = sysConfigMapper.updateByPrimaryKeySelective(sysConfig);
            count += result;
        }
        redisUtil.delete(SYS_CONFIG + type);
        return count;
    }

    @Override
    public int update(SysConfig sysConfig) {
        int count = sysConfigMapper.updateByPrimaryKeySelective(sysConfig);
        redisUtil.delete(SYS_CONFIG + sysConfig.getType());
        return count;
    }
}
